package ui;


import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;

import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLCapabilities;
import javax.swing.JFrame;

import logic.CanvasHandler;
import logic.GraphGenerator;
import logic.Visualization;

import data.Drawable;
import data.Edge;
import data.Node;


public class testVisualisation extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private GLCanvas canvas;
	private CanvasHandler canvasHandler;
	private Visualization vis;
	public testVisualisation(){
		super();
		this.setBounds(0, 0, 800, 600);
		GLCapabilities cap = new GLCapabilities();
		canvas = new GLCanvas(cap);
		canvas.setSize(2000, 2000);
		
		//generate new graph
		GraphGenerator.Graph graph = GraphGenerator.Graph.load("graph10.txt");
		LinkedList<Node> nodeList = (LinkedList<Node>) graph.getNodeList();
		HashMap<Integer, LinkedList<Node>> neighborList = graph.getNeighbors();
		
		//LinkedList<Node> nodeList = new LinkedList<Node>();
		LinkedList<Drawable> drawableList = new LinkedList<Drawable>();
		
		//draw rings
		//Vector3 ringColor = new Vector3(0.0f, 0.0f, 1.0f);
		
		//draw edges
		//Vector3 edgeColor = new Vector3(1.0f, 0.0f, 1.0f);
		Iterator<Node> nodeIt = nodeList.iterator();
		while (nodeIt.hasNext()){
			Node node = nodeIt.next();
			Iterator<Node> nghbr = neighborList.get(node.getID()).iterator();
			while (nghbr.hasNext())
			{
				Node nghbrNode = nghbr.next();
				drawableList.add(new Edge(node, nghbrNode));
			}
		}	

		//draw nodes
		nodeIt = nodeList.iterator();
		while (nodeIt.hasNext()){
		//for(int i = 0; i < 1000; i++){
			//Node node = new Node(i, 10, "Node"+i);
			Node node = nodeIt.next();		
			
			node.setAngle((float)(Math.random()*Math.PI*2));
			node.setOldAngle((float)(Math.random()*Math.PI*2));
			node.setRing((int)(Math.random()*10));
			node.setOldRing((int)(Math.random()*10));
			
			//nodeList.add(node);
			nodeList.set(node.getID(), node);
			drawableList.add(node);
		}
		
		
		canvasHandler = new CanvasHandler(drawableList);
		canvas.addGLEventListener(canvasHandler);
		canvas.addMouseMotionListener(canvasHandler);
		canvas.addMouseListener(canvasHandler);
		canvas.addMouseWheelListener(canvasHandler);
		canvas.addKeyListener(canvasHandler);
		canvas.setVisible(true);
		this.getContentPane().add(canvas);
		
		vis = new Visualization();
		Visualization.setFocusNode(nodeList.get((int)(nodeList.size()*Math.random())));
		vis.start();
		
		this.addWindowListener(new WindowListener(){
			@Override
			public void windowActivated(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			@Override
			public void windowClosed(WindowEvent e) {
				// TODO Auto-generated method stub
			}
			@Override
			public void windowClosing(WindowEvent e) {
				// TODO Auto-generated method stub
				//System.out.println("exited");
				vis = null;
				System.exit(NORMAL);
			}
			@Override
			public void windowDeactivated(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			@Override
			public void windowDeiconified(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			@Override
			public void windowIconified(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			@Override
			public void windowOpened(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
	}
	public static void main(String [] args){
		testVisualisation gui = new testVisualisation();
		gui.setVisible(true);
		while(true){
			gui.update();
		}
	}
	public void update(){
		canvas.update(null);
	}
}
